<template>
  <div>
    <el-table
      :data="store.getters['goods/getGoodsList']"
      border
      row-key="id"
      default-expand-all
      style="width: 100%"
    >
      <el-table-column prop="id" label="商品编号" />
      <el-table-column prop="goodsname" label="商品名称" />
      <el-table-column prop="price" label="商品价格" />
      <el-table-column label="市场价格" prop="market_price">
        <!-- <template v-slot="item">
          <div>
            <el-icon v-if="item.row.icon">
              <component :is="item.row.icon"></component>
            </el-icon>
          </div>
        </template> -->
      </el-table-column>
      <el-table-column prop="url" label="图片">
        <template v-slot="item">
          <div>
            <!-- <img
                            v-if="item.row.img"
                            class="goodsImg"
                            :src="item.row.img"
                            alt="图片"
                        /> -->
            <el-image
              v-if="item.row.img"
              style="width: 60px; height: 60px"
              :src="item.row.img"
              :preview-src-list="[item.row.img]"
            >
            </el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="是否新品">
        <template v-slot="item">
          <div>
            <el-tag v-if="item.row.isnew == 1" class="ml-2" type="success">是</el-tag>
            <el-tag v-else class="ml-2" type="danger">否</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="是否热卖">
        <template v-slot="item">
          <div>
            <el-tag v-if="item.row.ishot == 1" type="success">是</el-tag>
            <el-tag v-else type="danger">否</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="item">
          <div>
            {{ item.row.status == 1 ? "启用" : "禁用" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <!-- 
            #default
            #default === slot=""
            slot-scope === v-slot=""
         -->
        <template v-slot="item">
          <el-button type="primary" size="small" @click="edit(item.row.id)"
            >编辑</el-button
          >
          <el-button type="danger" size="small" @click="del(item.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="changePage"
      background
      layout="prev, pager, next"
      :total="store.getters['specs/getCount']"
      :page-size="store.getters['specs/getSize']"
    />
  </div>
</template>

<script lang="ts" setup>
import { defineEmits } from "vue";
import { menulist, menudelete } from "../../request/api";
import { ElMessage, ElMessageBox } from "element-plus";
import { useStore } from "vuex";
import { goodsdelete } from "../../request/api";
// import store from "../../store";
let store = useStore();
let emit = defineEmits<{
  (e: "edit", value: number): void;
}>();
// 封装一个编辑事件
let edit = (id: number) => {
  //   console.log(id, "idddddddd");
  emit("edit", id);
};

let getGoods = () => {
  store.dispatch("goods/getGoodsListActions");
  store.dispatch("goods/getCountActions");
};
getGoods();
const count = () => {
  store.dispatch("goods/getCountActions");
};
count();
// 点击拿到当前页码
const changePage = (e: number) => {
  store.dispatch("goods/changePageActions", e);
};

// 删除事件
let del = (id: number) => {
  ElMessageBox.confirm("你确定要删除吗", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      goodsdelete({ id }).then((res) => {
        if (res.code == 200) {
          ElMessage.success(res.msg);
          getGoods();
        } else {
          ElMessage.error(res.msg);
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除取消",
      });
    });
};
</script>

<style lang="less" scoped></style>
